#  Copyright (C) 2006 Tobias Rundstrom <tru@xmms.org>

from PyQt4 import QtCore

class XMMSConnector(QtCore.QObject):
	def __init__(self, xmms, args):
		parent = args
		QtCore.QObject.__init__(self, parent)
		fd = xmms.get_fd()
		self.xmms = xmms
		self.xmms.set_need_out_fun(self.chkWr)

		self.rsock = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, self)
		self.connect(self.rsock, QtCore.SIGNAL("activated(int)"), self.do_read)
		self.rsock.setEnabled(True)
		
		self.wsock = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Write, self)
		self.connect(self.wsock, QtCore.SIGNAL("activated(int)"), self.do_write)
		self.wsock.setEnabled(False)

	def chkWr(self, i):
		#print "chkWr"
		if self.xmms.want_ioout():
			self.toggle_write(True)
		else:
			self.toggle_write(False)

	def toggle_write(self, bool):
		self.wsock.setEnabled(bool)

	def do_read(self, i):
		#print "read"
		self.xmms.ioin()

	def do_write(self, i):
		#print "write"
		self.xmms.ioout()
	
